package fjut.acm.coding.controller;

import fjut.acm.coding.entity.BugReport;
import fjut.acm.coding.entity.result.ResultCode;
import fjut.acm.coding.entity.result.ResultJson;
import fjut.acm.coding.service.BugReportedService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@CrossOrigin
@RequestMapping("/bug")
public class BugReportController {

    @Resource
    BugReportedService bugReportedService;

    /**
     * 报告bug
     *
     * @param username    用户名
     * @param currentPath 当前路径
     * @param type        0
     * @param title       BUG标题
     * @param text        BUG内容
     * @return resultJson
     */
    @PostMapping("/report")
    public ResultJson reportBug(@RequestParam("username") String username,
                                @RequestParam("currentPath") String currentPath,
                                @RequestParam(value = "type", defaultValue = "0") Integer type,
                                @RequestParam("title") String title,
                                @RequestParam("text") String text) {
        ResultJson resultJson = new ResultJson();
        BugReport bugReport = new BugReport();
        bugReport.setUsername(username);
        bugReport.setCurrentPath(currentPath);
        bugReport.setType(type);
        bugReport.setTitle(title);
        bugReport.setText(text);
        bugReport.setIsFixed(0);
        bugReport.setReportTime(new Date());
        int ans = bugReportedService.insert(bugReport);
        if (1 == ans) {
            resultJson.setStatus(ResultCode.REQUIRED_SUCCESS);
        } else {
            resultJson.setStatus(ResultCode.BUSINESS_FAIL);
        }
        return resultJson;
    }
}
